library(data.table)
library(rstanarm)
source("functions.R")
load("all_data.rda")
load("analysis_data.RData")
load("results-2023-08-04.RData")
load("results-2023-08-31.RData")
load("results-2024-03-07.RData")

# Table 1: Session Details ----
ad <- copy(all_data)
ad[, payoff := (320 + payoff) / 20]
tab <- ad[, .(
  `\\# Subjects` = length(unique(ID)),
  `\\# Pairs` = length(unique(Group)),
  `\\# Obs.` = 30 * length(unique(Group))
),
  .(Condition, Session)][c(1, 2, 4, 3, 5, 6)]
tab[Session %in% c(2, 4, 5, 6), Condition := ""]
tab2 <- cbind(data.table(Condition = "", Session = " "),
  rbind(colSums(tab[1:3, -(1:2)])))
tab3 <- cbind(data.table(Condition = "", Session = " "),
  rbind(colSums(tab[4:6, -(1:2)])))
tab4 <- cbind(data.table(Condition = "", Session = ""),
  rbind(colSums(tab[, -(1:2)])))
tab <- rbind(tab[1:3], tab2, tab[4:6], tab3, tab4)
tab[9, Condition := "Total"]
tab[, lineend := "\\\\"]
tab[Session %in% c(2, 5), lineend := "\\\\ \\vspace{.2em}"]
tab[Session %in% c(4, 6), lineend := "\\\\ \\vspace{.8em}"]
tab[, index := .I]
tab[, Condition := as.character(Condition)]
tab <- tab[, paste(.SD, collapse = "&"), index][,
  paste(gsub("&\\\\", "\\\\", V1, fixed = TRUE),
    collapse = "\n")]
tab <- c(
  "\\begin{table}[t] \\footnotesize\n",
  "\\begin{center}\n",
  "\\caption{Session Details}\n",
  "\\label{table-sessions} \\vspace{1em}\n",
  "\\begin{threeparttable}\n",
  "\\begin{tabular}{lcccc}  \\vspace{.1em}\n",
  "Condition & Session &\\# Subjects & \\# Pairs & \\# Obs.\\\\  \\hline\n",
  tab, "\n",
  "\\hline\n",
  "\\end{tabular}\n",
  "\\begin{tablenotes}
  \\item{\\footnotesize
  Groups were randomly assigned every round
  An observation is a single interaction.
  There were 30 observations per pair.
  }
  \\end{tablenotes}",
  "\\end{threeparttable}\n",
  "\\end{center}\n",
  "\\end{table}"
)
cat(tab)

# Table 2: Text Increases Distance from Equilibrium Predictions ----
make_reg_table(model_list = list(m_eqm_action_lo, m_eqm_action_hi),
  model_names = c("Low Shift", "High Shift"),
  coef_order = c(2, 1),
  coef_names = c("Intercept", "Text"),
  group_order = c(4, 2, 1, 3, 5),
  label = "table-eqm-action-prox",
  group_names = c("Receiver", "Sender", "Period", "Session"),
  title = "Text Increases Distance from Equilibrium Predictions",
  scalebox = 1,
  notes = "
  The table presents mixed effects linear models
  of the negative distance between equilibrium predictions
  for actions and those actually selected by subjects.
  The models includes random
  intercepts for session, period, sender, and receiver.
  $^*$ Zero is not included in the $95\\%$ interval.
  ")

# Table 3: Evidence for the Comparative Statics Prediction Persists ----
# make_reg_table(model_list = list(m_comp_stat_numeric, m_comp_stat_text),
#   model_names = c("Numeric", "Text"),
#   coef_order = c(2, 1),
#   group_order = c(4, 2:3, 1, 5),
#   coef_names = c("Intercept", "Low Shift"),
#   label = "table-comp-stat",
#   group_names = c("Period", "Receiver", "Sender", "Session"),
#   title = "Evidence for the Comparative Statics Prediction Persists",
#   scalebox = 1,
#   notes = "
#   The table presents mixed effects linear models
#   of the negative distance between targets and actions.
#   The models includes random
#   intercepts for session, period, sender, and receiver.
#   $^*$ Zero is not included in the $95\\%$ interval.
#   ")

make_reg_table(model_list = list(m_comp_stat_numeric, m_comp_stat_text,
  m_comp_stat_numeric2, m_comp_stat_text),
  model_names = c("Numeric", "Text", "Numeric", "Text"),
  coef_order = c(2, 3, 1),
  group_order = c(4, 2:3, 1, 5),
  coef_names = c("Intercept", "Low Shift", "Target below Cutoff"),
  label = "table-comp-stat2",
  group_names = c("Period", "Receiver", "Sender", "Session"),
  title = "Evidence for the Comparative Statics Prediction Persists",
  scalebox = 1,
  notes = "
  The table presents mixed effects linear models
  of the negative distance between targets and actions.
  The models includes random
  intercepts for session, period, sender, and receiver.
  $^*$ Zero is not included in the $95\\%$ interval.
  ")

# Table 4: Payoffs Depend on Communication Technology ----
make_reg_table(model_list = list(m_payoff_sender, m_payoff_receiver,
  m_pareto),
  model_names = c("Sender", "Receiver", "Pareto"),
  coef_order = c(2, 3, 4, 1),
  group_order = c(4, 2, 1, 3, 5),
  coef_names = c("Intercept", "Text", "Low Shift", "Text $\\times$ Low Shift"),
  label = "table-payoffs",
  group_names = c("Receiver", "Sender", "Period", "Session"),
  title = "Payoffs Depend on Communication Technology",
  scalebox = 1,
  notes = "
  The first two columns present mixed effects linear models
  of payoffs.
The last presents a similar model of an indicator for whether the
action selected was on the Pareto frontier, i.e., between the target and
the target + shift.
  The models includes random
  intercepts for session, period, sender, and receiver.
  $^*$ Zero is not included in the $95\\%$ interval.
  ")

# Table 5: Varieties of Text Messages ----
make_reg_table(model_list = list(m1, m2, m3, m4, m5, m6),
  model_names = c("Empty", "Precise", "Interval",
    "Noisy", "Honesty", "Fairness"),
  coef_order = c(2, 4, 3, 1),
  coef_names = c("Intercept", "Target", "Time", "Low Shift"),
  label = "table-text-messages-1",
  group_names = c("Period", "Sender", "Session"),
  group_order = c(3, 2, 1, 4),
  title = "Varieties of Text Messages",
  scalebox = 1,
  notes = "
  The table presents the results of mixed effects linear probability
  regression models.
  The models includes random
  intercepts for period and sender
  to control for the panel structure of the data.
  ")

# Table 6: What Gets Mentioned? ----
make_reg_table(model_list = list(m_mention_s_target, m_mention_r_target,
  m_mention_action, m_mention_midpoint),
  model_names = c("Sender's Target", "Receiver's Target", "Action", "Midpoint"),
  coef_order = c(2, 4, 3, 1),
  coef_names = c("Intercept", "Target", "Time", "Low Shift"),
  label = "table-mentions",
  group_names = c("Period", "Sender", "Session"),
  group_order = c(3, 2, 1, 4),
  title = "What Gets Mentioned?",
  scalebox = 1,
  notes = "
  The table presents the results of mixed effects linear probability
  regression models.
  The models includes random
  intercepts for session, period, and sender
  to control for the panel structure of the data.
  ")

# Table 7: Evidence for the Comparative Statics Prediction Persists ----
make_reg_table(model_list = list(
  m_target_message_proximity, m_message_target),
  model_names = c(""),
  coef_order = c(2:8, 1),
  group_order = c(1:4),
  coef_names = c("Intercept", "Low Shift", "Text", "Low Shift $\\times$ Text",
    "Target", "Target $\\times$ Low_Shift", "Target $\\times$ Text",
    "Target $\\times$ Low_Shift $\\times$ Text"),
  label = "table-target-message-prox",
  group_names = c("Sender", "Period", "Session"),
  title = "Evidence for the Comparative Statics Prediction Persists",
  scalebox = 1,
  notes = "
  The table presents mixed effects linear models
  of the negative distance between targets and actions.
  The models includes random
  intercepts for session, period, and sender.
  $^*$ Zero is not included in the $95\\%$ interval.
  ")

# Table A1: Varieties of Text Messages (robustness) ----
make_reg_table(model_list = list(m2, m2_r_less_than_60),
  model_names = c("All Observations", "$r<60$"),
  coef_order = c(2, 4, 3, 1),
  coef_names = c("Intercept", "Target", "Time", "Low Shift"),
  label = "table-text-messages-1",
  group_names = c("Period", "Sender", "Session"),
  group_order = c(3, 2, 1, 4),
  title = "Varieties of Text Messages",
  scalebox = 1,
  notes = "
  The table presents the results of mixed effects linear probability
  regression models.
  The models includes random
  intercepts for period and sender
  to control for the panel structure of the data.
  ")

# Table A2: What Gets Mentioned? (robustness) ----
make_reg_table(model_list = list(
  m_mention_r_target, m_mention_r_target_r_less_than_60),
  model_names = c("All Observations", "$r<60$"),
  coef_order = c(2, 4, 3, 1),
  coef_names = c("Intercept", "Target", "Time", "Low Shift"),
  label = "table-mentions",
  group_names = c("Period", "Sender", "Session"),
  group_order = c(3, 2, 1, 4),
  title = "What Gets Mentioned?",
  scalebox = 1,
  notes = "
  The table presents the results of mixed effects linear probability
  regression models.
  The models includes random
  intercepts for session, period, and sender
  to control for the panel structure of the data.
  ")
